package com.asia5b.a5mall.middleware.logic.viewmodel.member

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2019/12/20.
 */
class CollectFragmentVM :SimpleViewModel() {
   private var page = 1
   private var sumPage = 1000

    /**
     * type 0/1 商品/店铺
     */
    fun getDatas(refresh: Int, type: Int) {
        if (refresh <= 1) {
            page = 1
            sumPage = 1000
        }
        if (page > sumPage) {
            dismissLoadingBar()
            return
        }

        if (type == 0) {
            val observable = memberApi.getCollectionList(page)
            baseSubscribe(observable, SimpleNetParams(isShowLoading = (refresh==0)),Consumer { resultModel ->
                sumPage = resultModel.sumPage
                page = resultModel.page + 1//预备加载下一页
                getGoodsCollectListResultLiveData.value=resultModel
            })
        } else {
            val observable = memberApi.getCollectionShopList(page)
            baseSubscribe(observable,SimpleNetParams(isShowLoading = (refresh==0)), Consumer { resultModel ->
                sumPage = resultModel.sumPage
                page = resultModel.page + 1//预备加载下一页
//                providerView().getShopCollectListResult(resultModel.data)
                getShopCollectListResultLiveData.value=resultModel
            })
        }
    }

    val getGoodsCollectListResultLiveData= MutableLiveData<ResultModel<ArrayList<AbsGoodsModel>>>()
    val getShopCollectListResultLiveData= MutableLiveData<ResultModel<ArrayList<AbsShopModel>>>()
    val deleteCollectionsResultLiveData= MutableLiveData<ArrayList<Int>>()
    val deleteCollectionShopListResultLiveData= MutableLiveData<ArrayList<Int>>()


    /**
     * @param ids 多个商品id
     */
    fun deleteCollections(ids: ArrayList<Int>) {
        val goodsIds = TextUtils.join(",", ids)
        val observable = memberApi.deleteCollectionList(goodsIds)
        baseSubscribe(observable, Consumer { data ->
//            providerView().deleteCollectionsResult(ids)
            deleteCollectionsResultLiveData.value=ids
        })
    }

    /**
     * @param ids 多个店铺id
     */
    fun deleteCollectionShopList(ids: ArrayList<Int>) {
        val goodsIds = TextUtils.join(",", ids)
        val observable = memberApi.deleteCollectionShopList(goodsIds)
        baseSubscribe(observable, Consumer { data ->
//            providerView().deleteCollectionShopListResult(ids)
            deleteCollectionShopListResultLiveData.value=ids
        })
    }
}